<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户" prop="userId">
        <el-select v-model="queryParams.userId" clearable filterable placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.userId"
            :label="item.userName"
            :value="item.userId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="充值类型" prop="rechargeRecordType">
        <el-select v-model="queryParams.rechargeRecordType" placeholder="请选择充值类型" clearable size="small">
          <el-option label="请选择" value="" />
          <el-option label="VIP月卡" value="1" />
          <el-option label="VIP季卡" value="2" />
          <el-option label="VIP年卡" value="3" />
          <el-option label="VIP半年卡" value="8" />
          <el-option label="金币充值" value="7" />
          <el-option label="客服充值" :value="9" />

        </el-select>
      </el-form-item>
      <el-form-item label="订单号" prop="rechargeRecordOrder">
        <el-input
          v-model="queryParams.rechargeRecordOrder"
          placeholder="请输入订单号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="充值状态" prop="rechargeRecordState">
        <el-select v-model="queryParams.rechargeRecordState" placeholder="请选择" clearable size="small">
          <el-option label="请选择" value="" />
          <el-option label="未支付" value="未支付" />
          <el-option label="已支付" value="已支付" />

        </el-select>

      </el-form-item>

      <el-form-item label="充值时间" prop="rechargeRecordTime">
        <el-date-picker clearable size="small"
          v-model="queryParams.rechargeRecordTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择充值时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="充值方式" prop="rechargeRecordWay">

        <el-select v-model="queryParams.rechargeRecordWay" placeholder="请选择" clearable size="small">
          <el-option label="请选择" value="" />
          <el-option label="自助充值" value="1" />
          <el-option label="客服充值" value="2" />

        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['xqsp:record:add']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['xqsp:record:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['xqsp:record:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['xqsp:record:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="recordList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="spRechargeRecord.rechargeRecordId" />
      <el-table-column label="用户" align="center" prop="userName" />
      <el-table-column label="充值类型" align="center" prop="spRechargeRecord.rechargeRecordType" />
      <el-table-column label="订单号" align="center" prop="spRechargeRecord.rechargeRecordOrder" />
      <el-table-column label="充值状态" align="center" prop="spRechargeRecord.rechargeRecordState" />
      <el-table-column label="充值金额" align="center" prop="spRechargeRecord.rechargeRecordMoney" />
      <el-table-column label="充值时间" align="center" prop="spRechargeRecord.rechargeRecordTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.spRechargeRecord.rechargeRecordTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="充值方式" align="center" prop="spRechargeRecord.rechargeRecordWay" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['xqsp:record:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['xqsp:record:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改充值管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户id" />
        </el-form-item>
        <el-form-item label="充值类型" prop="rechargeRecordType">
          <el-select v-model="form.rechargeRecordType" placeholder="请选择充值类型">
            <el-option label="请选择" value="" />
            <el-option label="VIP月卡" :value="1" />
            <el-option label="VIP季卡" :value="2" />
            <el-option label="VIP年卡" :value="3" />
            <el-option label="VIP半年卡" :value="8" />
            <el-option label="金币充值" :value="7" />
            <el-option label="客服充值" :value="9" />
          </el-select>
        </el-form-item>
        <el-form-item label="订单号" prop="rechargeRecordOrder">
          <el-input v-model="form.rechargeRecordOrder" placeholder="请输入订单号" />
        </el-form-item>
        <el-form-item label="充值状态" prop="rechargeRecordState">
          <el-select v-model="form.rechargeRecordState" placeholder="请选择" clearable size="small">
            <el-option label="请选择" value="" />
            <el-option label="未支付" value="未支付" />
            <el-option label="已支付" value="已支付" />

          </el-select>
        </el-form-item>
        <el-form-item label="充值金额" prop="rechargeRecordMoney">
          <el-input v-model="form.rechargeRecordMoney" placeholder="请输入充值金额" />
        </el-form-item>
        <el-form-item label="充值时间" prop="rechargeRecordTime">
          <el-date-picker clearable size="small"
            v-model="form.rechargeRecordTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择充值时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="充值方式" prop="rechargeRecordWay">
          <el-input v-model="form.rechargeRecordWay" placeholder="请输入充值方式" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRecord, getRecord, delRecord, addRecord, updateRecord, exportRecord } from "@/api/xqsp/record";
import { listUsers} from "@/api/xqsp/users";
export default {
  name: "Record",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 充值管理表格数据
      recordList: [],
//用户下拉框
      options:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 50,
        userId: null,
        rechargeRecordType: null,
        rechargeRecordOrder: null,
        rechargeRecordState: null,
        rechargeRecordMoney: null,
        rechargeRecordTime: null,
        rechargeRecordWay: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getUserList();
  },
  methods: {

    //查询用户
    getUserList() {
      listUsers(this.queryParams).then(response => {
        this.options = response.rows;
      });
    },
    /** 查询充值管理列表 */
    getList() {
      this.loading = true;
      listRecord(this.queryParams).then(response => {
        this.recordList = response.rows;

        this.recordList.forEach(item=>{
          if (item.spRechargeRecord.rechargeRecordType==1){
            item.spRechargeRecord.rechargeRecordType="VIP月卡"
          }else if (item.spRechargeRecord.rechargeRecordType==2){
            item.spRechargeRecord.rechargeRecordType="VIP季卡"
          }else if (item.spRechargeRecord.rechargeRecordType==3){
            item.spRechargeRecord.rechargeRecordType="VIP年卡"
          }else if (item.spRechargeRecord.rechargeRecordType==7){
            item.spRechargeRecord.rechargeRecordType="金币充值"
          }
          else if (item.spRechargeRecord.rechargeRecordType==8){
            item.spRechargeRecord.rechargeRecordType="VIP半年卡"
          }
          else if (item.spRechargeRecord.rechargeRecordType==9){
            item.spRechargeRecord.rechargeRecordType="客服充值"
          }
          if (item.spRechargeRecord.rechargeRecordWay==1){
            item.spRechargeRecord.rechargeRecordWay="自助充值"
          }else if (item.spRechargeRecord.rechargeRecordWay==2){
            item.spRechargeRecord.rechargeRecordWay="客服充值"
          }
        });

        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        rechargeRecordId: null,
        userId: null,
        rechargeRecordType: null,
        rechargeRecordOrder: null,
        rechargeRecordState: null,
        rechargeRecordMoney: null,
        rechargeRecordTime: null,
        rechargeRecordWay: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.spRechargeRecord.rechargeRecordId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加充值管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const rechargeRecordId = row.spRechargeRecord.rechargeRecordId || this.ids
      getRecord(rechargeRecordId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改充值管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.rechargeRecordId != null) {
            updateRecord(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRecord(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      let tmpIds=[]
      if (this.ids.length>0){
        for (const id of this.ids) {
          tmpIds.push(id);
        }
      }else {
        tmpIds.push(row.spRechargeRecord.rechargeRecordId);
      }

      this.$confirm('是否确认删除充值管理编号为"' + tmpIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delRecord(tmpIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有充值管理数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportRecord(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
